﻿Public Class MEeditForm

#Region " D e k l a r a t i o n e n "
    Private BsMengeneinheiten As New BindingSource
#End Region

#Region " Private Sub ButtonExit_Click "
    Private Sub ButtonExit_Click(sender As Object, e As EventArgs) Handles ButtonExit.Click
        Me.DialogResult = DialogResult.OK
    End Sub
#End Region

#Region " Private Sub MEeditForm_Load "
    Private Sub MEeditForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.DGV.DataSource = BsMengeneinheiten
        LadeDaten()
    End Sub
#End Region

#Region " Private Sub ButtonNeu_Click "
    Private Sub ButtonNeu_Click(sender As Object, e As EventArgs) Handles ButtonNeu.Click
        Me.ButtonSpeichern.Enabled = True
        Me.TextBox1.Enabled = True
        Me.TextBox1.Focus()
    End Sub
#End Region

#Region " Private Sub ButtonSpeichern_Click "
    Private Sub ButtonSpeichern_Click(sender As Object, e As EventArgs) Handles ButtonSpeichern.Click
        If TextBox1.Text <> "" Then
            DB.InsertDeleteUpdate("INSERT INTO " & TBL.Mengeneinheiten.TBL & " (" & TBL.Mengeneinheiten.Name & ") VALUES ('" & Me.TextBox1.Text & "')")
            Me.TextBox1.Enabled = False
            Me.TextBox1.Text = ""
            Me.ButtonSpeichern.Enabled = False
            LadeDaten()
        Else
            MsgBox("Bitte einen Namen eingeben.")
        End If
    End Sub
#End Region

#Region " Private Sub LadeDaten "
    Private Sub LadeDaten()
        Dim meDaten As DataTable = DB.BildeDataSet("SELECT Name FROM " & TBL.Mengeneinheiten.TBL).Tables(0)
        Me.DGV.DataSource = meDaten
        meDaten.Columns(0).ColumnName = "Mengeneinheit"
        Me.DGV.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
        Me.BsMengeneinheiten.DataSource = meDaten
        Me.DGV.AutoResizeColumns()
    End Sub
#End Region

#Region " Private Sub ButtonLöschen_Click "
    Private Sub ButtonLöschen_Click(sender As Object, e As EventArgs) Handles ButtonLöschen.Click
        If DGV.SelectedRows IsNot Nothing Then
            Dim id As Integer = CInt(DGV.SelectedRows(0).Cells(0).Value)
            DB.InsertDeleteUpdate("DELETE FROM " & TBL.Mengeneinheiten.TBL & " WHERE " & TBL.Mengeneinheiten.Id & " = " & id)
            LadeDaten()
        End If
    End Sub
#End Region

End Class